package com.samuel.mybatis.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

public class StringSplitHandler implements TypeHandler {

	public Object getResult(ResultSet rs, String columnName) throws SQLException {
		String obj = rs.getString(columnName);
		if (null != obj && !"".equals(obj)) {
			String[] strArr = obj.split("#");
			StringSplit ss = new StringSplit();
			ss.setStr(strArr);
			return ss;
		}
		return null;
	}

	public Object getResult(CallableStatement ps, int column) throws SQLException {
		String obj = ps.getString(column);
		if (null != obj && !"".equals(obj)) {
			String[] strArr = obj.split("#");
			StringSplit ss = new StringSplit();
			ss.setStr(strArr);
			return ss;
		}
		return null;
	}

	public void setParameter(PreparedStatement ps, int columnIndex, Object parameter, JdbcType arg3) throws SQLException {
		StringSplit strArr = (StringSplit) parameter;
		StringBuffer result = new StringBuffer();
		for (String str : strArr.getStr()) {
			result.append(str);
		}
		ps.setString(columnIndex, result.toString());
	}
}
